package simplyrugby.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import simplyrugby.entity.Training;
import simplyrugby.mapper.TeamMapper;
import simplyrugby.mapper.TrainingMapper;
import simplyrugby.service.TrainingService;

import java.util.List;

/**
* @author admin
* @description 针对表【training(训练)】的数据库操作Service实现
* @createDate 2025-03-29 18:37:40
*/
@Service
@RequiredArgsConstructor
public class TrainingServiceImpl extends ServiceImpl<TrainingMapper, Training>
    implements TrainingService{

    private final TeamMapper teamMapper;
    @Override
    public Page<Training> list(Integer pageNum, Integer pageSize, Training training) {
        LambdaQueryWrapper<Training> wrapper = Wrappers.lambdaQuery(Training.class)
                .eq(!ObjectUtils.isEmpty(training.getId()), Training::getId, training.getId())
                .eq(!ObjectUtils.isEmpty(training.getTrainingTeamId()), Training::getTrainingTeamId, training.getTrainingTeamId())
                .like(!ObjectUtils.isEmpty(training.getTrainingName()), Training::getTrainingName, training.getTrainingName());


        Page<Training> page = new Page<>(pageNum, pageSize);
        this.page(page, wrapper);
        List<Training> list = page.getRecords().stream().map(u -> this.details(u.getId()) ).toList();
        page.setRecords(list);
        return page;

    }

    @Override
    public Training details(String id) {
        Training training = baseMapper.selectById(id);
        training.setTeam(teamMapper.selectById(training.getTrainingTeamId()));
        return training;
    }
}




